//
//  GQHRoutesView.m
//  Xanthium
//
//  Created by GuanQinghao on 08/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHRoutesView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"
#import "YYMyRouteCell.h"

@interface GQHRoutesView () <UITableViewDelegate,UITableViewDataSource>

@end


@implementation GQHRoutesView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    _qh_tableView.estimatedRowHeight = 50;
    _qh_tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return MAX(_qh_routeModelArray.count, 1);
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    return 50.0f;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_qh_routeModelArray && _qh_routeModelArray.count > 0) {
        
        static NSString *identifier = @"aCcell";
        YYMyRouteCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"YYMyRouteCell" owner:self options:nil].firstObject;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        }
        
        GQHRouteModel *routeModel = _qh_routeModelArray[indexPath.row];
        cell.routeModel = routeModel;
        return cell;
    } else {
        
        static NSString *identifier = @"bCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.userInteractionEnabled = NO;
        }
        
        cell.textLabel.text = @"暂无路线信息";
        cell.textLabel.textColor = kDarkGrayColor;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_qh_routeModelArray && _qh_routeModelArray.count > 0) {
        
        return YES;
    } else {
        
        return NO;
    }
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewRowAction *removeRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除"handler:^(UITableViewRowAction *_Nonnull action,NSIndexPath *_Nonnull indexPath) {
        
        if ([_qh_delegate respondsToSelector:@selector(qh_removeRowWithTableView:atIndexPath:)]) {
            
            [_qh_delegate qh_removeRowWithTableView:tableView atIndexPath:indexPath];
        }
    }];
    removeRowAction.backgroundColor = [UIColor redColor];
    
    return @[removeRowAction];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kWhiteColor;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kDefaultFont;
    titleLabel.text = @"全部路线";
    titleLabel.textColor = kAppRedColor;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.mas_equalTo(headerView).with.offset(20);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    
    UIView *separatorView = [[UIView alloc] init];
    separatorView.backgroundColor = kLightGrayColor;
    [headerView addSubview:separatorView];
    [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(headerView);
        make.right.mas_equalTo(headerView);
        make.bottom.mas_equalTo(headerView);
        make.height.mas_equalTo(1);
    }];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

@end
